import {ipc} from "@/utils/ipcRenderer";
import {proxyToData} from "@/utils/fun";

class IpcApi {

    constructor(prefix = "") {
        this.prefix = prefix
    }
    getPageList (params = {}) {
        return ipc.request(this.prefix + 'getPageList',proxyToData(params))
    }

    getList(params = {}){
        return ipc.request(this.prefix + 'getList', proxyToData(params))
    }

    /**
     * 读取
     * @returns
     */
    read(id) {
        return ipc.request(this.prefix + 'read',id)
    }
    create(params){
        let data = proxyToData(params)
        return ipc.request(this.prefix + 'create', data)
    }
    save(__, params){
        let data = proxyToData(params)
        return ipc.request(this.prefix + 'save', data)
    }

    delete(data){
        return ipc.request(this.prefix + "delete",proxyToData(data))
    }

    recovery(data){
        return ipc.request(this.prefix + "recovery",proxyToData(data))
    }

    /**
     * 真实删除
     * @returns
     */
    realDelete(data) {
        return ipc.request(this.prefix + "realDelete",proxyToData(data))
    }

    getRecyclePageList(params = {}) {
        return ipc.request(this.prefix + 'getRecyclePageList',proxyToData(params))
    }
}


function makeApi(prefix) {
    let obj = {}
    let api = new IpcApi(prefix)
    Object.getOwnPropertyNames(IpcApi.prototype).map(name => {
        if (typeof IpcApi.prototype[name] === 'function' && name !== "constructor") {
            obj[name] = function (...params) {
                return api[name](...params)
            }
        }
    })

    return obj
}

export {
    makeApi
}


